import { fileURLToPath, URL } from 'node:url'
import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import { createHtmlPlugin } from 'vite-plugin-html'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Unocss from 'unocss/vite'
import Components from 'unplugin-vue-components/vite'

// https://vite.dev/config/
export default defineConfig(({command, mode})=>{
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_BASE_API,VITE_APP_WEB_URL,VITE_APP_TITLE } = env;
  return {
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      Unocss(),
      createHtmlPlugin({
        inject: {
          data: {
            title:VITE_APP_TITLE || 'vite-app'
          },
        },
      }),
      AutoImport({
        imports: [
          'vue',
          'vue-router',
          'pinia',
          // '@vueuse/core',
        ],
        dts: true, // 自动生成 auto-imports.d.ts
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
    server: {
      host: true,
      open: true,
      proxy: {
        // https://cn.vitejs.dev/config/#server-proxy
        [VITE_APP_BASE_API]: {
          target: VITE_APP_WEB_URL,
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, '')
        }
      }
    },
  }
})


